<?php namespace Hdphp\Request;



//配置项处理
class Request{
	
	//配置集合
	public $items=array();

	public function __construct()
	{
	}

	public function get($var, $default = null, $filter = '')
	{
    //拆分，支持get.id  或 id
    $var = explode(".", $var);
    if (count($var) == 1) {
        array_unshift($var, 'request');
    }
    $var[0] = strtolower($var[0]);
    //获得数据并执行相应的安全处理
    switch (strtolower($var[0])) {
        case 'get' :
            $data = &$_GET;
            break;
        case 'post' :
            $data = &$_POST;
            break;
        case 'request' :
            $data = &$_REQUEST;
            break;
        case 'files' :
            $data = &$_FILES;
            break;
        case 'session' :
            $data = &$_SESSION;
            break;
        case 'cookie' :
            $data = &$_COOKIE;
            break;
        case 'server' :
            $data = &$_SERVER;
            break;
        case 'globals' :
            $data = &$GLOBALS;
            break;
        default :
            return ;
    }
    //没有执行参数如q("post.")时返回所有数据
    if (empty($var[1])) {
        return $data;
        //如果存在数据如$this->_get("page")，$_GET中存在page数据
    } else if (isset($data[$var[1]])) {
        //要获得参数如$this->_get("page")中的page
        $value = $data[$var[1]];

        //参数过滤函数
        if (is_string($filter) && !empty($filter)) {
            $filter = explode(",", $filter);
        }
        //是否存在过滤函数
        if (!empty($filter) && is_array($filter)) {
            //对数据进行过滤处理
            foreach ($filter as $func) {
                if (!function_exists($func))
                    continue;
                $value = is_array($value) ? array_map($func, $value) : $func($value);
            }
            $data[$var[1]] = $value;
            return $value;
        }
        return $value;

    } else {
        $data[$var[1]] = $default;
        return $default;
    }
}

	//获取客户端IP地址
	public function ip($type = 0)
	{
		$type = intval($type);
    	//保存客户端IP地址
		if (isset($_SERVER)) {
			if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
				$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
				$ip = $_SERVER["HTTP_CLIENT_IP"];
			} else {
				$ip = $_SERVER["REMOTE_ADDR"];
			}
		} else {
			if (getenv("HTTP_X_FORWARDED_FOR")) {
				$ip = getenv("HTTP_X_FORWARDED_FOR");
			} else if (getenv("HTTP_CLIENT_IP")) {
				$ip = getenv("HTTP_CLIENT_IP");
			} else {
				$ip = getenv("REMOTE_ADDR");
			}
		}
		$long = ip2long($ip);
		$clientIp = $long ? array($ip, $long) : array("0.0.0.0", 0);
		return $clientIp[$type];
	}

	//https请求
	public function isHttps()
	{
		if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
			return true;
		} elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
			return true;
		}
		return false;
	}



}